import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
//the delAgent servlet allows the user to delete an agent from the Database
//Agents that are assigned to a house cause an error message to display
public class delAgent extends HttpServlet
{
	public static dbapi conn;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//Get variable ID
		conn = new dbapi();
		conn.connect();
		out.println(htmlHeader.header);//prints the standard title bar
		if(!conn.connected)//if can't connect display error message
		{
			out.println("CANT CONNECT! <br>");
			out.println(conn.msg + "<br>");
			return;
		}
		int AgentID = Integer.parseInt(request.getParameter("id"));
		
		if(!conn.isAgentAssigned(AgentID))//if the agent is not assigned, enter secondary if/else loop
		{	
			boolean worked = conn.delAgent(AgentID);
		
			if (!worked)//if the delete did not work, display error message
			{
				out.println(conn.msg);
			}else{//else if it did work, say so and refresh to the agent list
				out.println("<header><meta http-equiv=\"refresh\" content=\"2;getAgents\"></header><body><p>Removed Agent Successfully...<p></body>");
			}
		}else{//else inform the user that the current agent is assigned to a house and cannot be deleted
				out.println("<header><meta http-equiv=\"refresh\" content=\"5;getAgents\"></header><body><p>This agent is assigned to a house on the market and cannot be deleted now.<p></body>");	
		}
		
		return;
	}
}
